function J = cost_function_new(X, U, e, data, P_load_k)
% =========================================================================
% cost_function_new.m - (V3-无软约束) NMPC的自定义成本函数
%
% 核心功能:
% 1. 计算氢燃料消耗、电池老化、燃料电池老化的等效成本。
% 2. 不包含对软约束的惩罚。
% =========================================================================

    persistent CostParams FCParams SimParams
    if isempty(CostParams)
        CostParams = evalin('base', 'CostParams');
        FCParams   = evalin('base', 'FCParams');
        SimParams  = evalin('base', 'SimParams');
    end
    
    dt = SimParams.Ts;
    J = 0; % 初始化总成本
    
    x_current = data.CurrentStates;

    for k = 1:data.PredictionHorizon
        
        if k == 1
            state_k = x_current;
        else
            state_k = X(k-1, :);
        end
        state_kp1 = X(k, :);
        
        SOC_k           = state_k(1);
        alpha_decay_k   = state_k(3);
        
        SOC_kp1         = state_kp1(1);
        P_fc_kp1        = state_kp1(2);
        alpha_decay_kp1 = state_kp1(3);
        
        % 1. 氢燃料等效消耗成本 C_H2
        mass_h2_consumed = CostParams.k_H2 * P_fc_kp1 * dt;
        cost_h2 = CostParams.p_H2 * mass_h2_consumed;

        % 2. 锂电池寿命损耗成本 C_bat_loss
        delta_soc = abs(SOC_kp1 - SOC_k);
        cost_per_soc_change = CostParams.C_bat_total / CostParams.N_cycle;
        cost_bat = cost_per_soc_change * delta_soc;

        % 3. 燃料电池老化成本 C_fc_age
        delta_alpha = alpha_decay_kp1 - alpha_decay_k;
        cost_fc = (CostParams.C_fc_total / CostParams.alpha_max) * delta_alpha;
        
        J = J + cost_h2 + cost_bat + cost_fc;
    end
end
